#include<iostream>
#include<vector>
using namespace std;
const int N = 6005;

int n, a[N], dp[N][2];
vector<int> tr[N];

int max(int x, int y) {return x>y?x:y;}

void dfs(int u, int fa) {
    dp[u][1] = a[u];
    for(int v : tr[u]) {
        if(v == fa) continue;
        dfs(v, u);
        dp[u][0] += max(dp[v][0], dp[v][1]);
        dp[u][1] += dp[v][0];
    }
}

int main() {
    bool vv[N] = {false};
    cin >> n;
    for(int i=1; i<=n; i++)  cin >> a[i];
    for(int i=1; i<n; i++) {
        int x, y;
        cin >> x >> y;
        vv[x] = true;
        tr[y].push_back(x);
    }
    int root = -1;
    for(int i=1; i<=n; i++) if(!vv[i])   {root = i;break;}
    dfs(root, -1);
    cout << max(dp[root][0], dp[root][1]);
    return 0;
} 